import java.util.Arrays;

public class QiPan {

    String[][] qizhuo = new String[16][16];

    /**
     * 无参构造方法：用" + "填充棋盘
     */
    public QiPan() {
        for (int i = 0; i < 16; i++) {
            for (int j = 0; j < 16; j++) {
                qizhuo[i][j] = " + ";
            }
        }
    }

    /**
     * draw方法：画出棋盘内容
     */
    public void draw() {
        for (int i = 0; i < 16; i++) {

            if (0 == i) {
                System.out.print(' ');
                System.out.print(' ');
                for (int k = 0; k < 16; k++) {
                    System.out.print(' ');
                    System.out.print(Integer.toHexString(k)); //16进制
                    System.out.print(' ');
                }
                System.out.println();
            }
            System.out.print(Integer.toHexString(i));
            System.out.print(' ');

            for (int j = 0; j < 16; j++) {
                System.out.print(qizhuo[i][j]);
            }
            System.out.println();
        }
    }

    /**
     * 在棋盘指定位置下棋
     *
     * @param row
     * @param colu
     * @param color
     * @return boolean -- true or false
     */
    public boolean play(int row, int colu, String color) {
        if (row < 0 || colu < 0) {
            System.out.println("超出棋盘范围了！！！");
            return false;
        }

        if (" + " == qizhuo[colu][row]) {
            // 将棋子（白棋#，黑棋@）放入棋盘中
            qizhuo[colu][row] = QiZi.valueOf(color).show();
            return true;
        } else {
            System.out.println("该位置已经有棋子了!!!");
            return false;
        }
    }

    /**
     * 判断在当前列中是否可以获胜
     *
     * @param row
     * @param colu
     * @param color
     * @return boolean -- true or false
     */
    public boolean isWinRow(int row, int colu, String color) {
        int minColu = colu - 4;
        int maxColu = colu + 4;
        //当落子的上下到达边界时，置为0或15
        if (minColu < 0) {
            minColu = 0;
        }
        if (maxColu > 15) {
            maxColu = 15;
        }


        //遍历九个格子若有五个相同颜色的棋子连线则为胜
        int sum = 0;
        for (int i = minColu; i <= maxColu; i++) {
            //横坐标（row）不变，纵坐标（colu）变
            String piece = qizhuo[i][row];
            if (piece == QiZi.valueOf(color).show()) {
                sum++;
                if (sum == 5) {
                    return true;
                }
            } else {
                sum = 0;   //若不为同颜色 将sum置为0
            }
        }
        return false;
    }

    /**
     * 判断在当前行中是否可以获胜
     *
     * @param row
     * @param colu
     * @param color
     * @return boolean -- true or false
     */
    public boolean isWinColu(int row, int colu, String color) {
        int minRow = row - 4;
        int maxRow = row + 4;
        if (minRow < 0) {
            minRow = 0;
        }
        if (maxRow > 15) {
            maxRow = 15;
        }

        int sum = 0;
        for (int i = minRow; i <= maxRow; i++) {
            //纵坐标（colu）不变，横坐标（row变）
            String piece = qizhuo[colu][i];
            if (piece == QiZi.valueOf(color).show()) {
                sum++;
                if (sum == 5) {
                    return true;
                }
            } else {
                sum = 0;
            }
        }
        return false;
    }


    /**
     * 判断左斜（\）是否获胜
     *
     * @param row
     * @param colu
     * @param color
     */
    public boolean isWinLeftObl(int row, int colu, String color) {
        int sum = 0;
        for (int i = row - 4; i <= row + 5; i++) {
            for (int j = colu - 4; j < colu + 5; j++) {
                if (i >= 0 && i <= 15 && j >= 0 && j <= 15 && row - colu == i - j) {
                    if (qizhuo[j][i] == QiZi.valueOf(color).show()) {
                        sum++;
                        if (sum == 5) {
                            return true;
                        }
                    } else {
                        sum = 0;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 判断右斜（/）是否获胜
     *
     * @param row
     * @param colu
     * @param color
     * @return boolean -- true or false
     */
    public boolean isWinRightObl(int row, int colu, String color) {
        int sum = 0;
        for (int i = row - 4; i <= row + 5; i++) {
            for (int j = colu - 4; j < colu + 5; j++) {
                if (i >= 0 && i <= 15 && j >= 0 && j <= 15 && row + colu == i + j) {
                    if (qizhuo[j][i] == QiZi.valueOf(color).show()) {
                        sum++;
                        if (sum == 5) {
                            return true;
                        }
                    } else {
                        sum = 0;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 五子棋判断输赢
     *
     * @param row
     * @param colu
     * @param color
     * @return boolean -- true or false
     */
    public boolean isWin(int row, int colu, String color) {
        if (row < 0 || colu < 0) {
            System.out.println("超出棋盘范围了！！！");
            return false;
        }

        if (isWinColu(row, colu, color) || isWinRow(row, colu, color) || isWinLeftObl(row, colu, color) || isWinRightObl(row, colu, color)) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        QiPan qiPan = new QiPan();
        qiPan.draw();
    }
}
